#!/bin/tcsh -f
# set verbose

# Check Number of Args
if ( $#argv != 3 ) then
    echo "Usage: $0 env hmmdir tgtdir";
    exit 1;
endif
 
# Read the environment file
if ( ! -f $1 ) then
   echo "$0: cannot find environment file $1";
   exit 1;
endif

source $1;

if ( ! -d $2 || ! -f $2/MODELS ) then
   echo "$0: hmm model $2 does not exist";
   exit 1;
endif
    
# Create/Clear Target Directory
set tgt = $3;

# Check settings
if ( ! $?rmlib ) then
echo rmlib not set; exit 1;
endif
if ( ! $?HLMCONFIG ) then
echo HLMCONFIG not set; exit 1;
endif
if ( ! $?HDCONFIG ) then
echo HDCONFIG not set; exit 1;
endif
if ( ! $?HDMODCONFIG ) then
echo HDMODCONFIG not set; exit 1;
endif
if ( ! $?HLRCONFIG ) then
echo HLRCONFIG not set; exit 1;
endif
if ( ! $?TRAINDATALIST ) then
echo TRAINDATALIST not set; exit 1;
endif
if ( ! $?TRAINMLF ) then
echo TRAINMLF not set; exit 1;
endif
if ( ! $?HDVOCAB ) then
echo HDVOCAB not set; exit 1;
endif
if ( ! $?HLMORDER ) then
echo HLMORDER not set; exit 1;
endif
if ( ! $?HLMNNEWWORD ) then
echo HLMNNEWWORD not set; exit 1;
endif
if ( ! $?HLMBUFSIZE ) then
echo HLMBUFSIZE not set; exit 1;
endif
if ( ! $?HLMUGFLR ) then
echo HLMUGFLR not set; exit 1;
endif
if ( ! $?HLMNGRAMCUTOFF ) then
echo HLMNGRAMCUTOFF not set; exit 1;
endif
if ( ! $?HLMDISCOUNT ) then
echo HLMDISCOUNT not set; exit 1;
endif
if ( ! $?HDGENBEAM ) then
echo HDGENBEAM not set; exit 1;
endif
if ( ! $?HDGSCALE ) then
echo HDGSCALE not set; exit 1;
endif
if ( ! $?HDIMPROB ) then
echo HDIMPROB not set; exit 1;
endif
if ( ! $?HDTOKENS ) then
echo HDTOKENS not set; exit 1;
endif
if ( ! $?HLRGSCALE ) then
echo HLRGSCALE not set; exit 1;
endif
if ( ! $?HLRIMPROB ) then
echo HLRIMPROB not set; exit 1;
endif
if ( ! $?HLRMRGDIR ) then
echo HLRMRGDIR not set; exit 1;
endif
if ( ! $?HLRGENBEAM ) then
echo HLRGENBEAM not set; exit 1;
endif

cd $tgt;

set data =  ( `basename ${TRAINMLF} | awk -F\. '{print $1}'` );

mkdir -p numnets dennets; 
mkdir -p  lattices/num lattices/den;

set HLROPTS = ( -A -D -V -T ${HLRTRACE} -q tvalr -C ${HLRCONFIG} -w -s ${HLRGSCALE} -p ${HLRIMPROB} )
set HDOPTS  = ( -A -D -V -T ${HDTRACE} -t ${HDGENBEAM} -s ${HDGSCALE} -p ${HDIMPROB} -n ${HDTOKENS} -C ${HDCONFIG} )

foreach i ( `cat lib/flists/spkr.list` )

    echo Processing speaker ${i} with `cat lib/flists/${i}.scp | wc -l | awk '{print $1}'` utterances:;
    echo "";

    mkdir -p numnets/${i}; 
    HLRescore ${HLROPTS} -n lib/lms/bg.${data} -I ${TRAINMLF} -f -i numnets/${i}/bg.${i}.mlf -l numnets/${i}/ -S lib/flists/${i}.labscp ${HDVOCAB} >& numnets/${i}/LOG; 
    bzip2 -f numnets/${i}/LOG;

    mkdir -p lattices/num/${i}; 
    HDecode.mod ${HDOPTS} -C ${HDMODCONFIG} -i lattices/num/${i}/num.${i}.mlf -H ${2}/MODELS -d ${2} -q tvaldm -o M -z lat -w -L numnets/${i}/ -l lattices/num/${i}/ -X lat -S lib/flists/${i}.scp ${HDVOCAB} ${HMMLIST} >& lattices/num/${i}/LOG; 
    bzip2 -f lattices/num/${i}/LOG; 

    echo `ls lattices/num/${i} | egrep lat.gz | wc -l | awk '{print $1}'` numerator lattices generated for speaker ${i} ...;
    echo "";

    mkdir -p lattices/den/${i}; 
    HDecode.mod ${HDOPTS} -C ${HDMODCONFIG} -i lattices/den/${i}/den.${i}.mlf -H ${2}/MODELS -d ${2} -q tvaldm -o M -z lat -w -L dennets/${i}/ -l lattices/den/${i}/ -X lat -S lib/flists/${i}.scp ${HDVOCAB} ${HMMLIST} >& lattices/den/${i}/LOG; 
    bzip2 -f lattices/den/${i}/LOG;

    echo `ls lattices/den/${i} | egrep lat.gz | wc -l | awk '{print $1}'` denominator lattices generated for speaker ${i} ..; 
    echo "";
end

find lattices/num/ -name '*.lat.gz' | awk -F\/ '{print $NF}' | sed 's:.lat.gz::g' | sort -u > tmp.num; 
find lattices/den/ -name '*.lat.gz' | awk -F\/ '{print $NF}' | sed 's:.lat.gz::g' | sort -u > tmp.den; 
join tmp.num tmp.den > tmp.all; 
fgrep -f tmp.all ${TRAINDATALIST} | awk -F\/ '{printf("%s=%s\n", $NF, $0);}' > ${rmlib}/flists/${data}.lat.scp; 
rm -fr tmp.num tmp.den tmp.all;

echo "";
echo Phone lattices generated for `cat ${rmlib}/flists/${data}.lat.scp | wc -l | awk '{print $1}'` out of `cat ${rmlib}/flists/${data}.scp | wc -l | awk '{print $1}'` utterances in $PWD/lattices/ ...;
echo "";
echo "New scp file for discriminative training saved to ${rmlib}/flists/${data}.lat.scp";
echo "";
